package org.fing.edu.uy.admin.managedBean;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import org.fing.edu.uy.admin.factory.ManagerFactory;
import org.fing.edu.uy.admin.utils.FacesUtils;
import org.fing.edu.uy.admin.utils.MindmapUtils;
import org.fing.edu.uy.esbadp.core.AdpFlowInterface;
import org.fing.edu.uy.esbadp.core.AdpFlowTree;
import org.fing.edu.uy.esbadp.core.entity.DMRegisteredService;
import org.fing.edu.uy.esbadp.core.entity.GenericTreeNode;
import org.primefaces.event.SelectEvent;
import org.primefaces.model.mindmap.MindmapNode;

@ViewScoped
@ManagedBean(name="serviceFlowController")
public class ServiceFlowController implements Serializable {

	private static final long serialVersionUID = 1L;

	private List<DMRegisteredService> services;	
	private Long serviceID;
	private boolean selectView;
	
	private MindmapNode root;  
	private MindmapNode selectedNode;
	private long expiredOnSecs;

	/** Creates a new instance of ServiceController */
	public ServiceFlowController() {

		try {
			serviceID = FacesUtils.getLongRequestParam("id");
			if (serviceID != null) {
				selectView = false;
			} else {
				selectView = true;
				services = ManagerFactory.getServiceManager().getAllVirtualServices();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} 
	}

	private void loadGraph(MindmapNode parent, GenericTreeNode<AdpFlowInterface> tree) {
		MindmapNode addNode = null;
		
		if(parent == null){
			if(tree.getData() != null){
				root = MindmapUtils.createMindmapNode(tree.getData(),false);
			}
		}else{
			if(tree.getData() != null){
				addNode = MindmapUtils.createMindmapNode(tree.getData(),true);
				parent.addNode(addNode);
			}
		}

    	for (GenericTreeNode<AdpFlowInterface> node : tree.getChildren()) {    		
    		if(addNode == null){
    			loadGraph(root, node);
    		}else{
    			loadGraph(addNode, node);
    		}
        }
	}

	

	@PostConstruct
	public void init() {
		
		expiredOnSecs = -100;
		
		if(serviceID != null && serviceID >= 0){
			try{
				AdpFlowTree tree = ManagerFactory.getServiceManager().getFlowTreeForService(serviceID);
				Date now = new Date();
				Date exp = tree.getExpiredDate();
				
				if(tree.isDefaultADP()){
					expiredOnSecs = -1;
				}else{
					expiredOnSecs =  (exp.getTime() - now.getTime()) /1000 ;
				}				
				
				root = null;
				loadGraph(root, tree.getFlowTree());
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}else{
			root = null;
		}
	}

	public List<DMRegisteredService> getServices() {
		return services;
	}

	public void setServices(List<DMRegisteredService> services) {
		this.services = services;
	}

	public Long getServiceID() {
		return serviceID;
	}

	public void setServiceID(Long serviceID) {
		this.serviceID = serviceID;
	}

	public boolean isSelectView() {
		return selectView;
	}

	public void setSelectView(boolean selectView) {
		this.selectView = selectView;
	}

	public void reload() {
		init();
	}
     
    public MindmapNode getRoot() {  
        return root;  
    }  

    public MindmapNode getSelectedNode() {
        return selectedNode;
    }
    public void setSelectedNode(MindmapNode selectedNode) {
        this.selectedNode = selectedNode;
    }
    

    public long getExpiredOnSecs() {
		return expiredOnSecs;
	}

	public void setExpiredOnSecs(long expiredOnSecs) {
		this.expiredOnSecs = expiredOnSecs;
	}

	@SuppressWarnings("unused")
    public void onNodeSelect(SelectEvent event) {
		MindmapNode node = (MindmapNode) event.getObject(); 
    }
    
    public void onNodeDblselect(SelectEvent event) {
        this.selectedNode = (MindmapNode) event.getObject();        
    }
	
}
